// 拼接文件前缀
import {showLoading} from "@/utils/loading";
import {TagsViewModule} from "@/store/modules/tags-view";
import {removeAllCache} from "@/utils/storage";
import {refreshUserInfo} from "@/utils/userKit";
import router from "@/router/router";
import {IMAGE_BASE_URL} from "@/aconst/const";

const BASE_URL = IMAGE_BASE_URL;
// * 上传的图片字段格式是JSON数组字符串  "[{"id":"257540941466832896","url":"/virus/static/image/0A16EDD7C86E736D1CC1FD5D73A55C1B.jpg"}]"
/**
 * 文件数组添加前缀
 *
 *  @param array    服务端返回文件格式-字符串
 *         array    "[{"id":"257540941466832896","url":"/virus/static/image/0A16EDD7C86E736D1CC1FD5D73A55C1B.jpg"}]"
 *
 * @return newArray 经过方法拼接后--返回数组
 *         newArray [ {"id":"257540941466832896", "url":"http://192.168.50.54:8101/virus/static/image/0A16EDD7C86E736D1CC1FD5D73A55C1B.jpg", "original":"/virus/static/image/0A16EDD7C86E736D1CC1FD5D73A55C1B.jpg"}]
 *
 */
export const addBaseUrl = (array: any, key: any = null) => {

    let newArray: any = [];
    if (!array) {
        return newArray;
    }
    if (typeof array == "string") {
        array = JSON.parse(array);
    }
    for (let obj of array) {
        if(key){
            obj.url = (obj[key]);
            newArray.push(obj);
        }else{
            obj.original = (obj.url);
            obj.url = (BASE_URL + obj.url);
            newArray.push(obj);
        }

    }
    return newArray;
};

/**
 * 切换账号时调用
 */
export const switchAccount = (res: any) => {
    showLoading("加载中....");
    TagsViewModule.delAllCachedViews();
    TagsViewModule.delAllViews();
    removeAllCache();
    refreshUserInfo(res);
    setTimeout(() => {
        router.push({path: "/index"});
        setTimeout(() => {
            window.location.reload();
        }, 100);
    }, 100);

};
